package persistent.prestige.console.netty.dubbo.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import persistent.prestige.console.netty.dubbo.net.RequestFrameDecoder;
import persistent.prestige.console.netty.dubbo.net.ResponseFrameEncoder;

public class Server {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
		NioEventLoopGroup workGroup = new NioEventLoopGroup();
		
		try {
			ServerBootstrap bootstrap = new ServerBootstrap();
			
			ChannelFuture serverChannelFuture = bootstrap.group(bossGroup, workGroup)
					//.childOption(ChannelOption.SO_BACKLOG, 512)
					.channel(NioServerSocketChannel.class)
					.childHandler( new ChannelInitializer<SocketChannel>() {

						@Override
						protected void initChannel(SocketChannel sc) throws Exception {
							// TODO Auto-generated method stub
							sc.pipeline().addLast(new RequestFrameDecoder())
									     .addLast(new ResponseFrameEncoder())
									     .addLast(new DispatchHandler());
							
						}
						
					}).bind("127.0.0.1", 9080);
			
			
			System.out.println("服务器启动并在9080端口监听");
			serverChannelFuture.channel().closeFuture().sync();
			
			System.out.println("通道被关闭");
			
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			bossGroup.shutdownGracefully();
			workGroup.shutdownGracefully();
		}
		
		System.out.println("服务器停止");
		
		
		

	}
	
	
	
	

}
